Introducing Functions

OPENING QUESTION: What is a function and how is it used in computer programming?

There are actually several terms that are sometimes used that mean the same thing as a function:

  • procedures
  • subroutines (kinda old fashioned)
  • routines (kinda rare)

OBJECTIVES:  I will work to edit two different programs that each use functions in a different way during today's class.

I will work with animation for the first time today.

Review

CALENDAR:

WORDS O' THE DAY:

  • Argument (A value 'sent in' to a function to be used there)
  • Parameters (A 'placeholder variable inside a function that 'receives' the argument 'passed into' the function)

WORK O' THE DAY

Functions are a massively important aspect of programming in EVERY programming language. Functions are a great example of a software *abstraction*. By that I mean that functions take code that is:

  • too complex for your main problem. Moving that code to a separate function cleans up the main code and removes long lines of text that is cluttering up your main program.
  • repetitive. If you find yourself writing or copying/pasting the same code in several (or more) places in your code you should copy that code off into a function. It helps keep your main program clean and easy to follow.
  • very specific and/or difficult to write. Putting it off into a separate function makes it easier to debug all by itself

Functions are usually looked at/processed by the system (the compiler!) FIRST and are read into memory before the rest of your program is processed by the compiler. Some languages require that functions to be defined FIRST in your code. Other programs make you place functions at the END of your code. Still other programs don't really care if you place them first or last!

HERE is an example of a basic function. Please take a few moments and FULLY comment that code. Notice that the code is a bit weak. The onEvent listeners are not set particularly well. For example just starting to type causes the "Data Entry Received" msg to fire off. Can you make it work a bit more cleanly?

HERE is a more advanced animation program. Notice the use of the timedLoop function!!!